package TeamShadowCry.Source.Project.Process.Packs;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

import TeamShadowCry.Source.Project.MainInstance;
import TeamShadowCry.Source.Project.Process.Codec;
import TeamShadowCry.Source.Project.Process.ProcessInstance.ProcessInstance;

public class tsc_assm implements Codec{
	private int inFor(int a){
		if (a<0){
			return 128+a*-1;
		}else{
			return a;
		}
	}
	@Override
	public String name() {
		// TODO Auto-generated method stub
		return "TSC ASSM";
	}

	@Override
	public boolean UseLine() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int toReadSize() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public int aktion(byte[] val, byte[] Ram, byte[] extRam, byte[] all, InputStream fs, MainInstance given,ProcessInstance pr) throws IOException {
		int returning = 1;
		int cmd = -1;
		int valu = -1;
		int valu2 = -1;
		int ReadPlus = 0;
		if (val.length>0){
			ReadPlus=0;
			if (inFor(val[0])==0){
				ReadPlus+=2;
				while (inFor(given.readNext(pr,1)[0])==0){
					ReadPlus++;
				}
				cmd = 0;
				while (ReadPlus>0){
					cmd+=Integer.parseInt(Integer.toString(cmd, 2)+Integer.toString(inFor(given.readNext(pr,1)[0]), 2), 2);
					ReadPlus--;
				}
			}else{
				cmd=inFor(val[0]);
			}
			int constant = inFor(given.readNext(pr,1)[0]);
			ReadPlus=0;
			if (constant==0){
				ReadPlus+=2;
				while (inFor(given.readNext(pr,1)[0])==0){
					ReadPlus++;
				}
				valu = 0;
				while (ReadPlus>0){
					valu+=Integer.parseInt(Integer.toString(valu, 2)+Integer.toString(inFor(given.readNext(pr,1)[0]), 2), 2);
					ReadPlus--;
				}
			}else{
				valu=constant;
			}
			constant = inFor(given.readNext(pr,1)[0]);
			ReadPlus=0;
			if (constant==0){
				ReadPlus+=2;
				while (inFor(given.readNext(pr,1)[0])==0){
					ReadPlus++;
				}
				valu2 = 0;
				while (ReadPlus>0){
					valu+=Integer.parseInt(Integer.toString(valu2, 2)+Integer.toString(inFor(given.readNext(pr,1)[0]), 2), 2);
					ReadPlus--;
				}
			}else{
				valu2=constant;
			}
			/*
			 *
			 * 
			 * 
			 */
			if (cmd>0&&valu>0&&valu>0){
				cmd--;
				valu--;
				valu2--;
				//jmp Target IGNORED
				if (cmd==0){
					System.out.println("Jump to: "+valu);
					fs.reset();
					fs.skip(valu);
				}
				//mov Target Value
				if (cmd==1){
					System.out.println("Move in "+valu+" :"+valu2);
					Ram[valu] = (byte) Integer.parseInt(Integer.toString(valu2,2), 2);
				}
				//mov Target Source
				if (cmd==2){
					System.out.println("Move in "+valu+" : R["+valu2+"]="+Ram[valu2]);
					Ram[valu] = Ram[valu2];
				}
				//add Target Value
				if (cmd==3){
					System.out.println("Add to R["+valu+"]= "+Ram[valu]+" following number "+valu2);
					Ram[valu]+=valu2;
				}
				//add Target Source
				if (cmd==4){
					System.out.println("Add to R["+valu+"]="+Ram[valu]+" the following number R["+valu2+"]="+Ram[valu2]);
					Ram[valu]+=Ram[valu2];
				}
			}
		}else{
			System.out.println("Process is going to be Closed");
			returning = 0;
		}
		return returning;
	}
	@Override
	public int aktion(byte[] val, byte[] Ram, byte[] extRam, byte[] all, BufferedReader fs,
			MainInstance given, ProcessInstance pr) throws IOException {
		// TODO Auto-generated method stub
		return 0;
	}

}
